using System;
using System.Collections.Generic;
using System.Text;

namespace SingletonApp
{
    class Program
    {
        static void myReceive(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("MSG " + i.ToString() + ": " + args[i]);
            }
        }

        static void Main(string[] args)
        {
            // test if this is the first instance and register receiver, if so.
            if(SingletonController.IamFirst(new SingletonController.ReceiveDelegate(myReceive)))
            {
                // OK, this is the first instance, now run whatever you want ...
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("Hi " + i.ToString());
                    System.Threading.Thread.Sleep(1000);
                }
            }
            else
            {
                // send command line args to running app, then terminate
                SingletonController.Send(args);
            }

            SingletonController.Cleanup();
        }
    }
}
